define(['vue','config','common','../model/upLoadImg'],function(Vue,config,utils,upLoadImg){
	'use strict';
	
	var TransferView = new Vue({
		el:'#app',
		data:{
			userCode:'',
			consumptionPath:'',
			onprogress:'',
			onprogressAgain:'',

			fileList: {
				consumptionPath: '',
			},
			submitBtn:false																								
		},
		created:function(){	
			utils.userToken();

			var _this = this;
			utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
				_this.userCode = res.data.userCode;
			},function(){
				$('.DataLoading').show();
			},function(){
				$('.DataLoading').hide();
			})
		},	
		
		methods:{
			//第一次上传
			selectPhoto:function(event){
				var id = event.currentTarget.id;
				var $target = $(event.currentTarget);
				var $targetPic = $(event.currentTarget).parents('.Again').find('.imgAgain');

				$targetPic.show();
				$target.parents('.report-file').hide();

				var pic = upLoadImg.showPic($targetPic,$target);
				// 选择完立即压缩
				this.getFiles(id);
			},
			//再次上传
			selectAgain:function(event){

				var $target = $(event.currentTarget);
				var $targetPic = $target.parent('.imgAgain');
				var pic = upLoadImg.showPic($targetPic,$target);
				$target.parents('.Again').find('.file-prew').addClass('remove');

				var id = $target.parents('.Again').find('.file-prew').attr("id");
				// 选择完立即压缩
				this.getFiles(id);

			},
			getFiles:function(ele){
				var files;
				var _this = this;
				var element = $('#'+ele);
				if(element.hasClass('remove')){
					files = element.parents('.Again').find('.inputAgain').get(0).files[0];
				}else{
					files = element.get(0).files[0];
				}
				
				if(!files){
					_this.fileList[ele] = '';
				}else{
					lrz(files,{width:640})
					.then(function (rst) {
			        _this.fileList[ele] = rst.base64;
			        })
			        .catch(function (err) {
			        _this.fileList[ele] = '';
			        })
			        .always(function () {
			        });
				}

			},
			submit:function(){
				var _this = this;

				var getPic = function(element,isLoading){
					var pic;
					if(isLoading === "t"){
						pic = $(element).find('.pic').attr('src');
					}else{
						pic = $(element).find('.pic');
					}
					return pic;
				}

				// var getFiles = function(element){
				// 	var files;
				// 	var element = $(element);
				// 	if(element.hasClass('remove')){
				// 		return files = element.parents('.Again').find('.inputAgain').get(0).files[0];
				// 	}else{
				// 		return files = element.get(0).files[0];
				// 	}
				// }

				// var filesList = {
				// 	consumptionPath: getFiles('#consumptionPath')
				// }

				var onprogress = function(evt){
					var loaded = evt.loaded;     //已经上传大小情况 
					var tot = evt.total;      	 //附件总大小 
					var per = Math.floor(100*loaded/tot);  //已经上传的百分比
				    $('.upLoadimg').css({'height':(100-per)+'%'});
				}

				if($.trim(getPic('#consumptionPathPic','t')) === ""){
					utils.appAlert('转款凭证不能为空');
                    return;
				}

				if(_this.fileList.consumptionPath == ''){
					utils.appAlert('图片处理中，请稍后...');
					return;
				}

				if (_this.submitBtn) {
					return;
				}

				_this.submitBtn = true;

				var formData = new FormData();
				$.each(_this.fileList,function(k,p){
					if(p){
						formData.append("imgStr",p);
					}
				})
				upLoadImg.ajax(formData,onprogress,function(res){

					var urls = res.urls;
					getPic('#consumptionPathPic').attr('src',urls[0]);
					
					_this.isFirstapply = true;
					utils.ajax(utils.getWebUrl('user/vip'),{
						consumptionPath: urls[0]
					},'POST',function(res){
						utils.appAlert('<h2>提交成功</h2><br />正在审核您的会员升级申请,请稍后',function(){
							var userTypeId = utils.getData('userTypeId');
							if(userTypeId == 4){
								window.location.href='userInfo.html';
							}else{
								window.location.href='shopInfo.html';
							}
						})
					},null,null,null,function(res){
						_this.submitBtn = false;
						utils.appAlert(res.msg);
					})

				},null,null,null,function(res){
					_this.submitBtn = false;
					utils.appAlert(res.msg);
				})

				// var format = upLoadImg.uploadFile(filesList);
				// upLoadImg.ajax(format,onprogress,function(res){
				// 	var urls = res.urls;
				// 	getPic('#consumptionPathPic').attr('src',urls[0]);

				// 	utils.ajax(utils.getWebUrl('user/vip'),{
				// 		consumptionPath: urls[0]
				// 	},'POST',function(res){
				// 		utils.appAlert('<h2>提交成功</h2><br />正在审核您的会员升级申请,请稍后',function(){
				// 			var userTypeId = utils.getData('userTypeId');
				// 			if(userTypeId == 4){
				// 				window.location.href='userInfo.html';
				// 			}else{
				// 				window.location.href='shopInfo.html';
				// 			}
				// 		})
				// 	})
				// })

				/*if($.trim(getPic('#consumptionPathPic','t')) === ""){
					utils.appAlert('转款凭证不能为空');
                    return;
				}

				if(_this.onprogress !== ''){
					utils.appAlert('图片上传中，请稍等...');
                    return;
				}

				if(_this.onprogressAgain !== ''){
					utils.appAlert('图片上传中，请稍等...');
                    return;
				}
				//console.log(getPic('#consumptionPath'))
				utils.ajax(utils.getWebUrl('user/vip'),{
					consumptionPath: getPic('#consumptionPath')
				},'POST',function(res){
					utils.appAlert('<h2>提交成功</h2><br />正在审核您的会员升级申请,请稍后',function(){
						window.location.href="userInfo.html";
					})
				})*/
			}
		}
		
	})

	return TransferView;
});